using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;
using TMPro;

public class EnemyView : MonoBehaviour
{
    [Header("系统引用")]
    public EnemyData enemyData;

    [Header("组件引用")]
    public SpriteRenderer imageSR;
    public TMP_Text HPText;
    public TMP_Text AttackText;

    [Header("动画设置")]
    public float hpChangeAnimDuration = 0.3f;
    public Color damageColor = Color.red;
    public Color healColor = Color.green;
    public Color normalColor = Color.white;

    void Start()
    {
        InitializeEnemyView();
    }

    private void InitializeEnemyView()
    {
        // 自动查找缺失的引用
        if (enemyData == null)
        {
            enemyData = GetComponent<EnemyData>();
            if (enemyData == null)
            {
                Debug.LogError("EnemyView: 未找到EnemyData！");
                return;
            }
            else
            {
                Debug.Log($"EnemyView: 自动找到EnemyData: {enemyData.gameObject.name}");
            }
        }

        // 订阅事件
        enemyData.OnHPChanged += OnHPChanged;

        // 立即更新显示
        UpdateHPDisplay(enemyData.currentHP);
        UpdateAttackDisplay(enemyData.attackPower);

        if (DamagePopupController.Instance != null && enemyData != null)
        {
            DamagePopupController.Instance.RegisterTargetView(enemyData, this);
        }

        Debug.Log($"EnemyView 初始化完成 - {enemyData.displayName}");
    }

    /// <summary>
    /// HP变化事件处理
    /// </summary>
    private void OnHPChanged(int newHP)
    {
        Debug.Log($"EnemyView: {enemyData.displayName} HP变化 {enemyData.currentHP} -> {newHP}");

        // 更新HP显示
        UpdateHPDisplay(newHP);

        // 播放HP变化动画
        StartCoroutine(PlayHPChangeAnimation(newHP));
    }

    /// <summary>
    /// 更新HP显示
    /// </summary>
    private void UpdateHPDisplay(int currentHP)
    {
        if (HPText != null)
        {
            HPText.text = $"{currentHP}/{enemyData.maxHP}";

            // 根据HP百分比改变颜色
            float hpPercent = (float)currentHP / enemyData.maxHP;
            if (hpPercent <= 0.3f)
                HPText.color = Color.red;
            else if (hpPercent <= 0.6f)
                HPText.color = Color.yellow;
            else
                HPText.color = Color.green;
        }
    }

    /// <summary>
    /// 更新攻击力显示
    /// </summary>
    private void UpdateAttackDisplay(int attackPower)
    {
        if (AttackText != null)
        {
            AttackText.text = "ATK: " + attackPower.ToString();
        }
    }

    /// <summary>
    /// HP变化动画
    /// </summary>
    private IEnumerator PlayHPChangeAnimation(int newHP)
    {
        if (HPText == null) yield break;

        // 计算HP变化量
        int hpChange = newHP - enemyData.currentHP;

        if (hpChange != 0)
        {
            // 设置颜色
            Color targetColor = hpChange > 0 ? healColor : damageColor;
            Color originalColor = HPText.color;
            HPText.color = targetColor;

            // 缩放动画
            Vector3 originalScale = HPText.transform.localScale;
            Vector3 targetScale = originalScale * 1.2f;

            float elapsedTime = 0f;
            while (elapsedTime < hpChangeAnimDuration)
            {
                HPText.transform.localScale = Vector3.Lerp(targetScale, originalScale, elapsedTime / hpChangeAnimDuration);
                elapsedTime += Time.deltaTime;
                yield return null;
            }

            // 恢复
            HPText.transform.localScale = originalScale;
            HPText.color = originalColor;
        }
    }

    /// <summary>
    /// 处理受伤效果
    /// </summary>
    public void OnDamageTaken(int damageAmount)
    {
        // 受伤特效
        if (imageSR != null)
        {
            StartCoroutine(PlayDamageFlash());
        }

        Debug.Log($"EnemyView: {enemyData.displayName} 受到 {damageAmount} 点伤害");
    }

    /// <summary>
    /// 受伤闪烁效果
    /// </summary>
    private IEnumerator PlayDamageFlash()
    {
        if (imageSR == null) yield break;

        Color originalColor = imageSR.color;
        imageSR.color = Color.red;

        yield return new WaitForSeconds(0.1f);

        imageSR.color = originalColor;
    }

    /// <summary>
    /// 处理治疗效果
    /// </summary>
    public void OnHealed(int healAmount)
    {
        // 治疗特效
        if (imageSR != null)
        {
            StartCoroutine(PlayHealFlash());
        }

        Debug.Log($"EnemyView: {enemyData.displayName} 恢复 {healAmount} 点生命值");
    }

    /// <summary>
    /// 治疗闪烁效果
    /// </summary>
    private IEnumerator PlayHealFlash()
    {
        if (imageSR == null) yield break;

        Color originalColor = imageSR.color;
        imageSR.color = Color.green;

        yield return new WaitForSeconds(0.1f);

        imageSR.color = originalColor;
    }

}
